﻿using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.UI;
using Camc.Quality.Authorization.Users;
using Camc.Quality.Bop;
using Camc.Quality.Common.CommonDate;
using Camc.Quality.DataCenter.ProductionReview;
using Camc.Quality.DataDictionary;
using Camc.Quality.Materials;
using Camc.Quality.Quality;
using Camc.Quality.RecordTable.FormClass;
using Camc.Quality.StoreManagement;
using Camc.Quality.StoreManagement.StoragePut;
using Camc.Quality.StoreManagement.StoragePut.Dto;
using Camc.Quality.StoreManagement.StoreInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Camc.Quality.StoreManagement.StoreEnum;

namespace Camc.Quality.DataCenter
{
    public class ProductionReviewAppService : QualityAppServiceBase
    {
        private readonly IRepository<StorePutContent, Guid> _storePutContentRepository;
        private readonly IRepository<StorePutRecord, Guid> _storePutRecordRepository;
        private readonly IRepository<StoreStock, Guid> _storeStockRepository;
        private readonly IRepository<StoreStockContent, Guid> _storeStockContentRepository;
        private readonly IRepository<User, long> _userRepository;
        private readonly IRepository<MaterialsContent, Guid> _materialsContentRepository;
        private readonly IRepository<MaterialsNode, Guid> _materialsNodeRepository;
        private readonly IRepository<Sys_Code, Guid> _sysCodeRepository;
        private readonly MaterialsAppService _materialsAppServiceRepository;
        private readonly IRepository<LoadPieceEntity, Guid> _loadPieceEntityRepository;
        private readonly IRepository<WorkStepEntity, Guid> _workStepEntityRepository;
        private readonly IRepository<ProcessesEntity, Guid> _processesEntityRepository;
        private readonly IRepository<BopNode, Guid> _bopNodeRepository;
        private readonly IRepository<TechniqueEntity, Guid> _techniqueEntityRepository;
        private readonly IRepository<DeliveryOutRecord, Guid> _deliveryOutRecordRepository;
        private readonly IRepository<DeliveryDocument, Guid> _deliveryDocumentRepository;
        private readonly IRepository<CancellingStocksDocument, Guid> _cancellingStocksDocumentRepository;
        private readonly IRepository<CancellingRecord, Guid> _cancellingRecordRepository;
        public ProductionReviewAppService(
            IRepository<StorePutContent, Guid> storePutContentRepository,
            IRepository<StorePutRecord, Guid> storePutRecordRepository,
            IRepository<StoreStock, Guid> storeStockRepository,
            IRepository<StoreStockContent, Guid> storeStockContentRepository,
            IRepository<User, long> userRepository,
            IRepository<MaterialsContent, Guid> materialsContentRepository,
            IRepository<MaterialsNode, Guid> materialsNodeRepository,
            IRepository<Sys_Code, Guid> sysCodeRepository,
            MaterialsAppService materialsAppServiceRepository,
            IRepository<LoadPieceEntity, Guid> loadPieceEntityRepository,
            IRepository<WorkStepEntity, Guid> workStepEntityRepository,
            IRepository<ProcessesEntity, Guid> processesEntityRepository,
            IRepository<BopNode, Guid> bopNodeRepository,
            IRepository<TechniqueEntity, Guid> techniqueEntityRepository,
            IRepository<DeliveryDocument, Guid> deliveryDocumentRepository,
            IRepository<DeliveryOutRecord, Guid> deliveryOutRecordRepository,
            IRepository<CancellingStocksDocument, Guid> cancellingStocksDocumentRepository,
            IRepository<CancellingRecord, Guid> cancellingRecordRepository
            )
        {
            _storePutContentRepository = storePutContentRepository;
            _storePutRecordRepository = storePutRecordRepository;
            _storeStockRepository = storeStockRepository;
            _storeStockContentRepository = storeStockContentRepository;
            _userRepository = userRepository;
            _materialsContentRepository = materialsContentRepository;
            _materialsNodeRepository = materialsNodeRepository;
            _sysCodeRepository = sysCodeRepository;
            _materialsAppServiceRepository = materialsAppServiceRepository;
            _loadPieceEntityRepository = loadPieceEntityRepository;
            _workStepEntityRepository = workStepEntityRepository;
            _processesEntityRepository = processesEntityRepository;
            _bopNodeRepository = bopNodeRepository;
            _techniqueEntityRepository = techniqueEntityRepository;
            _deliveryDocumentRepository = deliveryDocumentRepository;
            _deliveryOutRecordRepository = deliveryOutRecordRepository;
            _cancellingStocksDocumentRepository = cancellingStocksDocumentRepository;
            _cancellingRecordRepository = cancellingRecordRepository;
        }
        /// <summary>
        /// 根据放行单号、图号、规格、产品名称、产品编号获取合格证号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<LabelCollectString> GetQualifiedNumberListForReview(GetQualifiedNumberOrReleaseOrderInput input)
        {
            var storePutContent = _storePutContentRepository.GetAll()
                .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder)
                .WhereIf(!string.IsNullOrEmpty(input.ProductDrawingNo), d => d.ProductDrawingNo == input.ProductDrawingNo)
                .WhereIf(!string.IsNullOrEmpty(input.ProductName), d => d.ProductName == input.ProductName)
                .WhereIf(!string.IsNullOrEmpty(input.Module), d => d.ProductDrawingNo == input.Module)
                .WhereIf(!string.IsNullOrEmpty(input.ProductNumber), d => d.ProductNumber.Contains(input.ProductNumber));
            List<LabelCollectString> collections = new List<LabelCollectString>();
            if (storePutContent != null)
            {
                collections = (from spc in storePutContent
                               select new LabelCollectString
                               {
                                   Label = spc.QualifiedNumber,
                                   Value = spc.QualifiedNumber
                               }).ToList();
            }
            return collections;
        }
        /// <summary>
        /// 根据合格证号、图号、规格、产品名称、产品编号获取放行单号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<LabelCollectString> GetReleaseOrderrListForReview(GetQualifiedNumberOrReleaseOrderInput input)
        {
            var storePutContent = _storePutContentRepository.GetAll()
                .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                .WhereIf(!string.IsNullOrEmpty(input.ProductDrawingNo), d => d.ProductDrawingNo == input.ProductDrawingNo)
                .WhereIf(!string.IsNullOrEmpty(input.ProductName), d => d.ProductName == input.ProductName)
                .WhereIf(!string.IsNullOrEmpty(input.Module), d => d.ProductDrawingNo == input.Module)
                .WhereIf(!string.IsNullOrEmpty(input.ProductNumber), d => d.ProductNumber.Contains(input.ProductNumber));
            List<LabelCollectString> collections = new List<LabelCollectString>();
            if (storePutContent != null)
            {
                collections = (from spc in storePutContent
                               select new LabelCollectString
                               {
                                   Label = spc.ReleaseOrder,
                                   Value = spc.ReleaseOrder
                               }).ToList();
            }
            return collections;
        }
        /// <summary>
        /// 入库记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<StorePutModelContentDto> GetStorePutRecordListForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            var storePutRecord = _storePutRecordRepository.GetAll();
            var queryStorePutContents = (from spr in storePutRecord
                                         from spc in _storePutContentRepository.GetAll()
                                         where spr.QualifiedNumber == spc.QualifiedNumber && spr.ReleaseOrder == spc.ReleaseOrder
                                         select new
                                         {
                                             Id = spc.Id,
                                             StorePutRecordId = spr.Id,
                                             StorePutNo = spc.StorePutNo,
                                             StoreId = spc.StoreId,
                                             StroeName = spc.StroeName,
                                             BinLocationId = spc.BinLocationId,
                                             BinLocation = spc.BinLocation,
                                             MaterialsContentId = spc.MaterialsContentId,
                                             ReleaseOrder = spr.ReleaseOrder,
                                             Remark = spc.Remark,
                                             StoreStateContent = spc.StoreStateContent,
                                             StorePutPeopleId = spr.StorePutPeopleId,
                                             StorePutTime = spr.StorePutTime,
                                             IdentifyingPeopleId = spc.IdentifyingPeopleId,
                                             IdentifyingTime = spc.IdentifyingTime,
                                             QualifiedNumber = spr.QualifiedNumber,
                                             ProductDrawingNo = spc.ProductDrawingNo,
                                             ProductName = spc.ProductName,
                                             Batch = spc.Batch,
                                             QualifiedTaskNumber = spc.QualifiedTaskNumber,
                                             Quantity = spr.Quantity,
                                             ProductNumberShow = spr.ProductNumber,
                                             ProductNumberType = spc.ProductNumberType,
                                             SpecialRecords = spr.SpecialRecords,
                                             MaterialBrandStatus = spc.MaterialBrandStatus,
                                             HeatNumber = spc.HeatNumber,
                                             Mthd = spc.Mthd,
                                             NonmetallicMaterialsBrand = spc.NonmetallicMaterialsBrand,
                                             BatchNo = spc.BatchNo,
                                             SuppressionDate = spc.SuppressionDate,
                                             DocumentNo = spc.DocumentNo,
                                             ManufacturingWorkshop = spc.ManufacturingWorkshop,
                                             ProductionWorkers = spc.ProductionWorkers,
                                             Foreman = spc.Foreman,
                                             Inspector = spc.Inspector,
                                             Date = spc.Date,
                                             QCCardNo = spr.QCCardNo,
                                             AssemblyTaskNumber = spc.AssemblyTaskNumber,
                                             StageMark = spc.StageMark,
                                             MaterialGrade = spc.MaterialGrade,
                                             MaterialStatus = spc.MaterialStatus,
                                             MaterialSpecification = spc.Module,
                                             StandardSpecification = spc.Module,
                                             TechnicalConditions = spc.TechnicalConditions,
                                             MaterialCertificate = spc.MaterialCertificate,
                                             CooperationCertificate = spc.CooperationCertificate,
                                             SupplyCompany = spc.SupplyCompany,
                                             NonMetallicManufacturingDate = spc.NonMetallicManufacturingDate,
                                             NonMetallicPressingDate = spc.NonMetallicPressingDate,
                                             ProductCode = spc.ProductCode,
                                             ProductCodeId = spc.ProductCodeId,
                                             SendTime = spc.SendTime,
                                             SendTimeId = spc.SendTimeId,
                                             Module = spc.Module,
                                             ExpirationDate = spc.ExpirationDate,
                                             UnitId = spc.UnitId,
                                             Unit = spc.Unit,
                                             TurnBatch = spc.TurnBatch,
                                             MaterialsProperty = spr.MaterialsProperty,
                                             WarehousMethod = spc.WarehousMethod,
                                             IsolationCode = spc.IsolationCode,
                                             IsolationReason = spc.IsolationReason,
                                             IsolationUserId = spc.IsolationUserId,
                                             IsolationUser = spc.IsolationUser,
                                             IsolationTime = spc.IsolationTime,
                                             CreatorUserId = spr.CreatorUserId,
                                             CreationTime = spr.CreationTime,
                                         })
                                         .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                         .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder);
            if (queryStorePutContents.Count() > 0)
            {
                var userTemp = _userRepository.GetAll().ToList();

                var materialsNode = _materialsNodeRepository.GetAll();
                List<StorePutModelContentDto> storePuts = new List<StorePutModelContentDto>();
                var query = queryStorePutContents.ToList();
                for (int i = 0; i < queryStorePutContents.Count(); i++)
                {
                    //var query = queryStorePutContents.ToList();
                    var materialsContents = _materialsContentRepository.FirstOrDefault(s => s.Id == query[i].MaterialsContentId);
                    if (materialsContents != null)
                    {
                        var storeStockContent = _storeStockContentRepository.GetAll().Where(s => s.StorePutContentId == query[i].Id);
                        StorePutModelContentDto storePut = new StorePutModelContentDto();
                        storePut.ProductCodeName = query[i].ProductCode;
                        storePut.ProductCode = query[i].ProductCodeId;
                        storePut.SendTimeName = query[i].SendTime;
                        storePut.SendTime = query[i].SendTimeId;
                        storePut.Id = query[i].Id;
                        storePut.StorePutRecordId = query[i].StorePutRecordId;
                        storePut.CreationTime = query[i].CreationTime;
                        storePut.CreatorUserId = query[i].CreatorUserId;
                        storePut.StorePutNo = query[i].StorePutNo;
                        storePut.StoreId = query[i].StoreId;
                        storePut.StroeName = query[i].StroeName;
                        storePut.BinLocationId = query[i].BinLocationId;
                        storePut.BinLocation = query[i].BinLocation;
                        storePut.MaterialsContentId = query[i].MaterialsContentId;
                        storePut.ReleaseOrder = query[i].ReleaseOrder;
                        storePut.Remark = query[i].Remark;
                        storePut.StorePutPeople = userTemp.FirstOrDefault(d => d.Id == query[i].CreatorUserId).Surname + userTemp.FirstOrDefault(d => d.Id == query[i].CreatorUserId).Name;
                        storePut.StoreStateContent = (int)query[i].StoreStateContent;
                        storePut.StorePutTime = query[i].StorePutTime;
                        storePut.IdentifyingPeopleId = query[i].IdentifyingPeopleId;
                        if (query[i].IdentifyingPeopleId != null)
                        {
                            storePut.IdentifyingPeople = userTemp.FirstOrDefault(d => d.Id == query[i].IdentifyingPeopleId).Surname + userTemp.FirstOrDefault(d => d.Id == query[i].IdentifyingPeopleId).Name;
                        }
                        storePut.IdentifyingTime = query[i].IdentifyingTime;
                        //合格证号
                        storePut.QualifiedNumber = query[i].QualifiedNumber;
                        //产品图号
                        storePut.ProductDrawingNo = query[i].ProductDrawingNo;
                        //产品名称
                        storePut.ProductName = query[i].ProductName;
                        //批次
                        storePut.Batch = query[i].Batch;
                        //合格证号任务
                        storePut.QualifiedTaskNumber = query[i].QualifiedTaskNumber;
                        //数量
                        storePut.Quantity = query[i].Quantity;
                        //产品编号(件号)
                        storePut.ProductNumberShow = query[i].ProductNumberShow;
                        //产品编号生成方式 0：自动带出，1：手动输入， 3：自动生成
                        storePut.ProductNumberType = (int)query[i].ProductNumberType;
                        //特殊记载
                        storePut.SpecialRecords = query[i].SpecialRecords;
                        //金属材料牌号状态
                        storePut.MaterialBrandStatus = query[i].MaterialBrandStatus;
                        //炉(批)号
                        storePut.HeatNumber = query[i].HeatNumber;
                        //化(试)验号
                        storePut.Mthd = query[i].Mthd;
                        //非金属材料牌号
                        storePut.NonmetallicMaterialsBrand = query[i].NonmetallicMaterialsBrand;
                        //批次号
                        storePut.BatchNo = query[i].BatchNo;
                        //压制日期
                        storePut.SuppressionDate = query[i].SuppressionDate;
                        //处理技术问题文件编号
                        storePut.DocumentNo = query[i].DocumentNo;
                        //制造车间
                        storePut.ManufacturingWorkshop = query[i].ManufacturingWorkshop;
                        //生产工人
                        storePut.ProductionWorkers = query[i].ProductionWorkers;
                        //工(组)长
                        storePut.Foreman = query[i].Foreman;
                        //检验员
                        storePut.Inspector = query[i].Inspector;
                        //日期
                        storePut.Date = query[i].Date;
                        //质控卡号
                        storePut.QCCardNo = query[i].QCCardNo;
                        //装配任务号
                        storePut.AssemblyTaskNumber = query[i].AssemblyTaskNumber;
                        //阶段标记
                        storePut.StageMark = query[i].StageMark;
                        //材料牌号
                        storePut.MaterialGrade = query[i].MaterialGrade;
                        //材料状态
                        storePut.MaterialStatus = query[i].MaterialStatus;
                        //材料规格
                        storePut.MaterialSpecification = query[i].MaterialSpecification;
                        //标准件规格
                        storePut.StandardSpecification = query[i].StandardSpecification;
                        //技术条件
                        storePut.TechnicalConditions = query[i].TechnicalConditions;
                        //材料合格证
                        storePut.MaterialCertificate = query[i].MaterialCertificate;
                        //协作合格证
                        storePut.CooperationCertificate = query[i].CooperationCertificate;
                        //供应单位
                        storePut.SupplyCompany = query[i].SupplyCompany;
                        //非金属制造日期
                        storePut.NonMetallicManufacturingDate = query[i].NonMetallicManufacturingDate;
                        //非金属压制日期
                        storePut.NonMetallicPressingDate = query[i].NonMetallicPressingDate;

                        ////型号(产品代号)
                        //storePut.ProductCode = query[i].ProductCodeId;
                        ////发次
                        //storePut.SendTime = query[i].SendTimeId;
                        //物料细分类别
                        storePut.MaterialCategoryId = materialsContents.MaterialCategoryId;
                        storePut.MaterialCategory = _materialsNodeRepository.FirstOrDefault(s => s.Id == materialsContents.MaterialCategoryId).MaterialTypeName;
                        //物料类别(有问题)
                        storePut.MaterialsNodeId = materialsContents.MaterialsNodeId;
                        storePut.MaterialsNode = _materialsNodeRepository.FirstOrDefault(s => s.Id == materialsContents.MaterialsNodeId).MaterialTypeName;
                        //规格型号
                        storePut.Module = query[i].Module;
                        //有效期止
                        if (query[i].ExpirationDate == "2099-01-01T00:00:00.000Z")
                        {
                            storePut.ExpirationDate = null;
                        }
                        else
                        {
                            storePut.ExpirationDate = query[i].ExpirationDate;
                        }
                        //单位Id
                        storePut.UnitId = query[i].UnitId;
                        //单位
                        storePut.Unit = query[i].Unit;
                        //转批次
                        storePut.TurnBatch = query[i].TurnBatch;
                        //物料属性
                        storePut.MaterialsProperty = (int)query[i].MaterialsProperty;
                        ////入库状态
                        //storePut.InputState = (int)query[i].InputState;
                        //入库方式
                        storePut.WarehousMethod = (int)query[i].WarehousMethod;
                        storePuts.Add(storePut);
                    }
                }
                //var Result = ObjectMapper.Map<List<StorePutModelContentDto>>(query);
                return storePuts;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 库存记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<StoreInfoDto> GetStoreInfoListForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            var userTemp = _userRepository.GetAll().ToList();
            var materialsNode = _materialsNodeRepository.GetAll();
            var storeStockList = (from spc in _storePutContentRepository.GetAll()
                                  join mc in _materialsContentRepository.GetAll()
                                  on spc.MaterialsContentId equals mc.Id
                                  where spc.StoreStateContent == StoreState.已入库
                                  select new StoreInfoDto
                                  {
                                      #region 仓库信息 
                                      Id = spc.Id,
                                      //入库单号
                                      StorePutNo = spc.StorePutNo,
                                      // 物料细料ID
                                      MaterialsContentId = spc.MaterialsContentId,
                                      // 仓库ID
                                      StoreId = spc.StoreId,
                                      // 仓库名称
                                      StroeName = spc.StroeName,
                                      // 储位ID
                                      BinLocationId = spc.BinLocationId,
                                      // 储位
                                      BinLocation = spc.BinLocation,
                                      // 放行单号
                                      ReleaseOrder = spc.ReleaseOrder,
                                      //备注
                                      Remark = spc.Remark,
                                      // 入库时间
                                      StorePutTime = spc.StorePutTime,
                                      // 入库人
                                      StorePutPeople = userTemp.FirstOrDefault(d => d.Id == spc.CreatorUserId).Surname + userTemp.FirstOrDefault(d => d.Id == spc.CreatorUserId).Name,
                                      //入库方式
                                      WarehousMethod = (int)spc.WarehousMethod,
                                      //物料入库状态 0待入库、1已入库
                                      StoreStateContent = (int)spc.StoreStateContent,
                                      // 确认人ID
                                      IdentifyingPeopleId = spc.IdentifyingPeopleId,
                                      // 确认人
                                      IdentifyingPeople = userTemp.FirstOrDefault(d => d.Id == spc.IdentifyingPeopleId).Surname + userTemp.FirstOrDefault(d => d.Id == spc.IdentifyingPeopleId).Name,
                                      // 确认时间
                                      IdentifyingTime = spc.IdentifyingTime,
                                      #endregion

                                      #region 合格证信息 
                                      // 合格证号
                                      QualifiedNumber = spc.QualifiedNumber,
                                      // 产品图号
                                      ProductDrawingNo = spc.ProductDrawingNo,
                                      // 产品名称
                                      ProductName = spc.ProductName,
                                      //产品编号(件号)
                                      ProductNumber = spc.ProductNumber,
                                      //产品编号(展示用)
                                      ProductNumberShow = spc.ProductNumberShow, //add 11/30
                                      // 批次
                                      Batch = spc.Batch,
                                      // 合格证号任务
                                      QualifiedTaskNumber = spc.QualifiedTaskNumber,
                                      // 特殊记载
                                      SpecialRecords = spc.SpecialRecords,
                                      // 金属材料牌号状态
                                      MaterialBrandStatus = spc.SpecialRecords,
                                      // 炉(批)号
                                      HeatNumber = spc.HeatNumber,
                                      // 化(试)验号
                                      Mthd = spc.Mthd,
                                      // 非金属材料牌号
                                      NonmetallicMaterialsBrand = spc.NonmetallicMaterialsBrand,
                                      // 批次号
                                      BatchNo = spc.BatchNo,
                                      // 压制日期
                                      SuppressionDate = spc.SuppressionDate,
                                      // 处理技术问题文件编号
                                      DocumentNo = spc.DocumentNo,
                                      // 制造车间
                                      ManufacturingWorkshop = spc.ManufacturingWorkshop,
                                      // 生产工人
                                      ProductionWorkers = spc.ProductionWorkers,
                                      // 工(组)长
                                      Foreman = spc.Foreman,
                                      // 检验员
                                      Inspector = spc.Inspector,
                                      // 日期
                                      Date = spc.Date,
                                      // 质控卡号
                                      QCCardNo = spc.QCCardNo,
                                      // 装配任务号
                                      AssemblyTaskNumber = spc.AssemblyTaskNumber,
                                      // 阶段标记
                                      StageMark = spc.StageMark,
                                      // 材料牌号
                                      MaterialGrade = spc.MaterialGrade,
                                      // 材料状态
                                      MaterialStatus = spc.MaterialStatus,
                                      // 材料规格
                                      MaterialSpecification = spc.Module,
                                      // 标准件规格
                                      StandardSpecification = spc.Module,
                                      // 技术条件
                                      TechnicalConditions = spc.TechnicalConditions,
                                      // 材料合格证
                                      MaterialCertificate = spc.MaterialCertificate,
                                      // 协作合格证
                                      CooperationCertificate = spc.CooperationCertificate,
                                      // 供应单位
                                      SupplyCompany = spc.SupplyCompany,
                                      // 非金属制造日期
                                      NonMetallicManufacturingDate = spc.NonMetallicManufacturingDate,
                                      // 非金属压制日期
                                      NonMetallicPressingDate = spc.NonMetallicPressingDate,
                                      #endregion

                                      #region 基础信息 
                                      // 型号(产品代号)
                                      ProductCodeName = spc.ProductCode,
                                      ProductCode = spc.ProductCodeId,
                                      // 发次
                                      SendTimeName = spc.SendTime,
                                      SendTime = spc.SendTimeId,
                                      //物料细分类别
                                      MaterialCategoryId = mc.MaterialCategoryId,
                                      MaterialCategory = materialsNode.FirstOrDefault(s => s.Id == mc.MaterialCategoryId).MaterialTypeName,
                                      //物料类别(有问题)
                                      MaterialsNodeId = mc.MaterialsNodeId,
                                      MaterialsNode = materialsNode.FirstOrDefault(s => s.Id == mc.MaterialsNodeId).MaterialTypeName,
                                      // 规格
                                      Module = spc.Module,
                                      // 有效期止
                                      ExpirationDate = spc.ExpirationDate,
                                      // 单位Id
                                      UnitId = spc.UnitId,
                                      // 单位
                                      Unit = spc.Unit,
                                      //转批次
                                      TurnBatch = spc.TurnBatch,
                                      // 物料属性
                                      MaterialsProperty = (int)spc.MaterialsProperty,
                                      // 装配任务号
                                      AssemblingTaskCode = spc.AssemblyTaskNumber,
                                      // 库存数量
                                      StoreQuality = _storeStockContentRepository.GetAll().Where(s => s.StorePutContentId == spc.Id && s.OutboundState != StoreStockContent.OutboundStates.出库).Count(),
                                      // 自由库存数量
                                      FreeStoreQuality = _storeStockContentRepository.GetAll().Where(s => s.StorePutContentId == spc.Id && s.IsPreSuit == StoreStockContent.IsSuits.否 && s.IsSuit == StoreStockContent.IsSuits.否 && s.OutboundState != StoreStockContent.OutboundStates.出库).Count(),
                                      // 可用预配套数量
                                      UsableQuantityStock = _storeStockContentRepository.GetAll().Where(s => s.StorePutContentId == spc.Id && s.IsPreSuit == StoreStockContent.IsSuits.是 && s.IsSuit == StoreStockContent.IsSuits.否 && s.OutboundState != StoreStockContent.OutboundStates.出库).Count(),
                                      // 配套数量
                                      SuiteStock = _storeStockContentRepository.GetAll().Where(s => s.StorePutContentId == spc.Id && s.IsSuit == StoreStockContent.IsSuits.是 && s.OutboundState != StoreStockContent.OutboundStates.出库).Count(),
                                      // 隔离单号
                                      IsolationCode = spc.IsolationCode,
                                      // 隔离原因
                                      IsolationReason = spc.IsolationReason,
                                      // 隔离人员
                                      IsolationUser = spc.IsolationUser,
                                      // 隔离时间
                                      IsolationTime = spc.IsolationTime,
                                      #endregion
                                      CreationTime = spc.CreationTime
                                  }).WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                   .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).ToList();

            foreach (StoreInfoDto storeInfoDto in storeStockList)
            {
                if (storeInfoDto.ExpirationDate == "2099-01-01T00:00:00.000Z")
                {
                    storeInfoDto.ExpirationDate = null;
                }
            }
            return storeStockList;
        }
        /// <summary>
        /// 配套记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<PreSuiteInfoDto> GetPreSuiteListForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            List<PreSuiteInfoDto> preSuiteInfoDtos = new List<PreSuiteInfoDto>();
            //已配套数据
            var tempData = (from ssc in _storeStockContentRepository.GetAll()
                            join spc in _storePutContentRepository.GetAll()
                            on ssc.StorePutContentId equals spc.Id
                            join lp in _loadPieceEntityRepository.GetAll()
                            on ssc.LoadDetailsId equals lp.Id
                            join ws in _workStepEntityRepository.GetAll()
                            on lp.WorkStepEntityId equals ws.Id
                            join p in _processesEntityRepository.GetAll()
                            on ws.ProcessesEntityId equals p.Id
                            join bn in _bopNodeRepository.GetAll()
                            on ws.TechniqueEntityId equals bn.TechniqueEntityId
                            join t in _techniqueEntityRepository.GetAll()
                            on ws.TechniqueEntityId equals t.Id
                            where ssc.IsSuit == StoreStockContent.IsSuits.是 && spc.QualifiedNumber == input.QualifiedNumber
                            select new PreSuiteInfoDto
                            {
                                ProductDrawingNo = ssc.ProductDrawingNo,
                                ProductName = ssc.ProductName,
                                Module = ssc.Module,
                                QualifiedNumber = ssc.QualifiedNumber,
                                ReleaseOrder = ssc.ReleaseOrder,
                                QCCardNo = spc.QCCardNo,
                                Batch = ssc.Batch,
                                AssemblyTaskNumber = spc.AssemblyTaskNumber,
                                ProductNumber = ssc.ProductNumber,
                                DesignatedModule = ssc.DesignatedModule,
                                DesignatedSendTime = ssc.DesignatedSendTime,
                                DesignatedXJ = ssc.DesignatedXJ,
                                DesignatedXJCode = ssc.DesignatedXJCode,
                                NodeDrawingNo = bn.NodeDrawingNo,
                                NodeName = bn.NodeName,
                                TechniqueName = t.TechniqueName,
                                TechniqueNumber = t.TechniqueNumber,
                                WorkStepNumber = ws.WorkStepNumber,
                                WorkStepNumberStr = "工步" + ws.WorkStepNumber.ToString(),//add 11/30
                                ProcessesSequence = "工序" + p.ProcessesSequence.ToString(),
                                ProcessesName = p.ProcessesName,
                                CompletePeople = ssc.CompletePeople,
                                CompleteTime = ssc.CompleteTime
                            })
                            .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                            .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).ToList();
            if (tempData.Count() > 0)
            {
                foreach (var item in tempData)
                {
                    preSuiteInfoDtos.Add(item);
                }
            }
            //未配套数据
            var storePutContent = (from ssc in _storeStockContentRepository.GetAll()
                                   join spc in _storePutContentRepository.GetAll()
                                   on ssc.StorePutContentId equals spc.Id
                                   where ssc.IsSuit == StoreStockContent.IsSuits.否 && spc.QualifiedNumber == input.QualifiedNumber
                                   select new PreSuiteInfoDto
                                   {
                                       ProductDrawingNo = ssc.ProductDrawingNo,
                                       ProductName = ssc.ProductName,
                                       Module = ssc.Module,
                                       QualifiedNumber = ssc.QualifiedNumber,
                                       ReleaseOrder = ssc.ReleaseOrder,
                                       QCCardNo = spc.QCCardNo,
                                       Batch = ssc.Batch,
                                       AssemblyTaskNumber = spc.AssemblyTaskNumber,
                                       ProductNumber = ssc.ProductNumber,
                                       DesignatedModule = ssc.DesignatedModule,
                                       DesignatedSendTime = ssc.DesignatedSendTime,
                                       DesignatedXJ = ssc.DesignatedXJ,
                                   })
                                   .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                   .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).Distinct().ToList();
            if (storePutContent.Count() > 0)
            {
                foreach (var item in storePutContent)
                {
                    preSuiteInfoDtos.Add(item);
                }
            }
            return preSuiteInfoDtos;
        }
        /// <summary>
        /// 出库记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<DeliveryOutRecordDto> GetDeliveryOutListForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            List<DeliveryOutRecordDto> outRecordDtos = new List<DeliveryOutRecordDto>();
            var deliveryOutList = from cc in _deliveryOutRecordRepository.GetAll()
                                  join mm in _deliveryDocumentRepository.GetAll()
                                  on cc.DeliveryDocumentId equals mm.Id
                                  join ssc in _storeStockContentRepository.GetAll()
                                  on cc.StoreStockContentId equals ssc.Id
                                  join spc in _storePutContentRepository.GetAll()
                                  on ssc.StorePutContentId equals spc.Id

                                  select new DeliveryOutRecordDto
                                  {
                                      ProductDrawingNo = ssc.ProductDrawingNo,
                                      ProductName = ssc.ProductName,
                                      Module = ssc.Module,
                                      QCCardNo = spc.QCCardNo,
                                      Batch = ssc.Batch,
                                      ProductNumber = cc.ProductNumber,
                                      AssemblyTaskNumber = mm.AssemblyTaskNumber,
                                      RetrievalState = (int)mm.RetrievalState,
                                      OutPeople = mm.OutPeople,
                                      StorePutTime = mm.StorePutTime,
                                      IdentifyingPeople = mm.IdentifyingPeople,
                                      IdentifyingTime = mm.IdentifyingTime,
                                      Remark = mm.Remark,
                                      WhereAbouts = mm.WhereAbouts,
                                      QualifiedNumber = cc.QualifiedNumber,
                                      ReleaseOrder = cc.ReleaseOrder

                                  };
            if (deliveryOutList.Count() > 0)
            {
                outRecordDtos = deliveryOutList.WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                               .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).ToList();
            }
            return outRecordDtos;
        }
        /// <summary>
        /// 退库记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<DeliveryAndCancelReviewRecordDto> GetCancellingListForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            List<DeliveryAndCancelReviewRecordDto> cancelReviewRecordDtos = new List<DeliveryAndCancelReviewRecordDto>();
            var cancelReviewRecord = (from cr in _cancellingRecordRepository.GetAll()
                                      join mm in _cancellingStocksDocumentRepository.GetAll()
                                      on cr.CancellingStocksId equals mm.Id
                                      join ssc in _storeStockContentRepository.GetAll()
                                      on cr.StoreStockContentId equals ssc.Id
                                      join spc in _storePutContentRepository.GetAll()
                                      on ssc.StorePutContentId equals spc.Id
                                      select new
                                      {
                                          Id = mm.Id,
                                          No = mm.CancellingStocksNo,
                                          StroeName = mm.StroeName,
                                          ProductDrawingNo = ssc.ProductDrawingNo,
                                          ProductName = ssc.ProductName,
                                          Module = ssc.Module,
                                          QualifiedNumber = mm.QualifiedNumber,
                                          ReleaseOrder = mm.ReleaseOrder,
                                          QCCardNo = spc.QCCardNo,
                                          Amount = spc.Amount,
                                          ProductNumber = cr.ProductNumber,
                                          Remark = mm.CancellingReason,
                                          People = mm.CancellingUser,
                                          Time = mm.CancellingTime,
                                      }).WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                          .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder);
            var query = cancelReviewRecord.GroupBy(s => s.Id)
                .Select(s => new DeliveryAndCancelReviewRecordDto
                {
                    No = s.First().No,
                    StroeName = s.First().StroeName,
                    ProductDrawingNo = s.First().ProductDrawingNo,
                    ProductName = s.First().ProductName,
                    Module = s.First().Module,
                    QualifiedNumber = s.First().QualifiedNumber,
                    ReleaseOrder = s.First().ReleaseOrder,
                    QCCardNo = s.First().QCCardNo,
                    Amount = s.First().Amount,
                    Quantity = s.Count(),
                    ProductNumber = string.Join(",", s.Select(c => c.ProductNumber)),
                    Remark = s.First().Remark,
                    People = s.First().People,
                    Time = s.First().Time
                });
            return query.ToList();
        }
        /// <summary>
        /// 隔离记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<DeliveryAndCancelReviewRecordDto> GetQuarantineRecordForReview(GetProductionReviewInput input)
        {
            var storeStockContent = (from ssc in _storeStockContentRepository.GetAll().Where(s => s.MaterialsProperty == MaterialsPropertys.隔离品)
                                     join spc in _storePutContentRepository.GetAll()
                                     on ssc.StorePutContentId equals spc.Id
                                     select new
                                     {
                                         Id = spc.Id,
                                         No = spc.IsolationCode,
                                         StroeName = ssc.StroeName,
                                         ProductDrawingNo = ssc.ProductDrawingNo,
                                         ProductName = ssc.ProductName,
                                         Module = ssc.Module,
                                         QualifiedNumber = ssc.QualifiedNumber,
                                         ReleaseOrder = ssc.ReleaseOrder,
                                         QCCardNo = spc.QCCardNo,
                                         Amount = spc.Quantity,
                                         Quantity = spc.Quantity,
                                         ProductNumber = ssc.ProductNumber,
                                         Remark = spc.IsolationReason,
                                         People = spc.IsolationUser,
                                         Time = spc.IsolationTime
                                     })
                .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder);
            var query = storeStockContent.GroupBy(s => s.Id)
                    .Select(s => new DeliveryAndCancelReviewRecordDto
                    {
                        No = s.First().No,
                        StroeName = s.First().StroeName,
                        ProductDrawingNo = s.First().ProductDrawingNo,
                        ProductName = s.First().ProductName,
                        Module = s.First().Module,
                        QualifiedNumber = s.First().QualifiedNumber,
                        ReleaseOrder = s.First().ReleaseOrder,
                        QCCardNo = s.First().QCCardNo,
                        Amount = s.First().Quantity,
                        Quantity = s.First().Quantity,
                        ProductNumber = string.Join(",", s.Select(c => c.ProductNumber)),
                        Remark = s.First().Remark,
                        People = s.First().People,
                        Time = s.First().Time
                    });
            return query.ToList();
        }
        /// <summary>
        /// 返修记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<DeliveryAndCancelReviewRecordDto> GetReturnRecordForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            List<DeliveryAndCancelReviewRecordDto> deliveryAndCancelReviewRecordDtos = new List<DeliveryAndCancelReviewRecordDto>();
            var deliveryAndCancelReviewRecordDto = (from cc in _deliveryOutRecordRepository.GetAll()
                                                    join mm in _deliveryDocumentRepository.GetAll()
                                                    on cc.DeliveryDocumentId equals mm.Id
                                                    join ssc in _storeStockContentRepository.GetAll()
                                                    on cc.StoreStockContentId equals ssc.Id
                                                    join spc in _storePutContentRepository.GetAll()
                                                    on ssc.StorePutContentId equals spc.Id
                                                    where mm.RetrievalState == OutboundRetrieval.返修
                                                    select new
                                                    {
                                                        Id = mm.Id,
                                                        No = mm.DeliveryNo,
                                                        StroeName = mm.StroeName,
                                                        ProductDrawingNo = ssc.ProductDrawingNo,
                                                        ProductName = ssc.ProductName,
                                                        Module = ssc.Module,
                                                        QualifiedNumber = mm.QualifiedNumber,
                                                        ReleaseOrder = mm.ReleaseOrder,
                                                        QCCardNo = spc.QCCardNo,
                                                        Amount = spc.Amount,
                                                        Quantity = mm.OutNum,
                                                        ProductNumber = cc.ProductNumber,
                                                        Remark = mm.Remark,
                                                        People = mm.operationPerson,
                                                        Time = mm.operationTime,
                                                        ScrapReason = mm.ScrapReason

                                                    })
                                                    .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                                    .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).ToList();
            if (deliveryAndCancelReviewRecordDto.Count() > 0)
            {
                var query = deliveryAndCancelReviewRecordDto.GroupBy(s => s.Id)
                                        .Select(s => new DeliveryAndCancelReviewRecordDto
                                        {
                                            No = s.First().No,
                                            StroeName = s.First().StroeName,
                                            ProductDrawingNo = s.First().ProductDrawingNo,
                                            ProductName = s.First().ProductName,
                                            Module = s.First().Module,
                                            QualifiedNumber = s.First().QualifiedNumber,
                                            ReleaseOrder = s.First().ReleaseOrder,
                                            QCCardNo = s.First().QCCardNo,
                                            Amount = s.First().Quantity,
                                            Quantity = s.First().Quantity,
                                            ProductNumber = string.Join(",", s.Select(c => c.ProductNumber)),
                                            Remark = s.First().Remark,
                                            People = s.First().People,
                                            Time = s.First().Time
                                        });
                return query.ToList();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 报废记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<DeliveryAndCancelReviewRecordDto> GetScrapRecordForReview(GetProductionReviewInput input)
        {
            if (input.QualifiedNumber == null && input.ReleaseOrder == null)
            {
                return null;
            }
            List<DeliveryAndCancelReviewRecordDto> deliveryAndCancelReviewRecordDtos = new List<DeliveryAndCancelReviewRecordDto>();
            var deliveryAndCancelReviewRecordDto = (from cc in _deliveryOutRecordRepository.GetAll()
                                                    join mm in _deliveryDocumentRepository.GetAll()
                                                    on cc.DeliveryDocumentId equals mm.Id
                                                    join ssc in _storeStockContentRepository.GetAll()
                                                    on cc.StoreStockContentId equals ssc.Id
                                                    join spc in _storePutContentRepository.GetAll()
                                                    on ssc.StorePutContentId equals spc.Id
                                                    where mm.RetrievalState == OutboundRetrieval.报废
                                                    select new
                                                    {
                                                        Id = mm.Id,
                                                        No = mm.DeliveryNo,
                                                        StroeName = mm.StroeName,
                                                        ProductDrawingNo = ssc.ProductDrawingNo,
                                                        ProductName = ssc.ProductName,
                                                        Module = ssc.Module,
                                                        QualifiedNumber = mm.QualifiedNumber,
                                                        ReleaseOrder = mm.ReleaseOrder,
                                                        QCCardNo = spc.QCCardNo,
                                                        Amount = spc.Amount,
                                                        Quantity = mm.OutNum,
                                                        ProductNumber = cc.ProductNumber,
                                                        Remark = mm.Remark,
                                                        People = mm.operationPerson,
                                                        Time = mm.operationTime,
                                                        ScrapReason = mm.ScrapReason

                                                    })
                                                    .WhereIf(!string.IsNullOrEmpty(input.QualifiedNumber), d => d.QualifiedNumber == input.QualifiedNumber)
                                                    .WhereIf(!string.IsNullOrEmpty(input.ReleaseOrder), d => d.ReleaseOrder == input.ReleaseOrder).ToList();
            if (deliveryAndCancelReviewRecordDto.Count() > 0)
            {
                var query = deliveryAndCancelReviewRecordDto.GroupBy(s => s.Id)
                                    .Select(s => new DeliveryAndCancelReviewRecordDto
                                    {
                                        No = s.First().No,
                                        StroeName = s.First().StroeName,
                                        ProductDrawingNo = s.First().ProductDrawingNo,
                                        ProductName = s.First().ProductName,
                                        Module = s.First().Module,
                                        QualifiedNumber = s.First().QualifiedNumber,
                                        ReleaseOrder = s.First().ReleaseOrder,
                                        QCCardNo = s.First().QCCardNo,
                                        Amount = s.First().Quantity,
                                        Quantity = s.First().Quantity,
                                        ProductNumber = string.Join(",", s.Select(c => c.ProductNumber)),
                                        Remark = s.First().Remark,
                                        People = s.First().People,
                                        Time = s.First().Time
                                    });
                return query.ToList();
            }
            else
            {
                return null;
            }
        }
    }
}
